<?php

require_once("Model/User.php");
require_once("Model/UserHandler.php");
require_once("Model/UserFileHandler.php");
require_once("Model/LoginHandler.php");

class InstallHandler {
		
	const DBHOST = "Host";
	const DBUSER = "User";
	const DBPASS = "Pass";
	const DB = "DB";
	
	//Directory and file
	private $m_fileName = "./Common/DBSettings.php";
	private $m_prefix;
	
	private $m_DBConnection;
	private $m_userHandler;
	private $m_userFileHandler;
	private $m_loginHandler;
	
	
	/**
	 * A function to create a DBSettings.php file filled with info given by user
	 * @Access Public
	 * @Param Array with settings submited by the handler
	 * @return Bool if document was created
	 */
	public function createDBSettingsDocument($a_settingsArray) {
		//Allow empty passwords
		if(!isset($a_settingsArray[self::DBPASS])){
			$a_settingsArray[self::DBPASS] = "";
		}
		
		$confString = "<?php
		/**
		* A User-created file
		* info commes from user
		* given to us by installation form
		*/
		
		class DBSettings{
			 const DBHOST = '". $a_settingsArray[self::DBHOST]."'; 
			 const DBUSER = '".$a_settingsArray[self::DBUSER]."'; 
			 const DBPASSWORD = '".$a_settingsArray[self::DBPASS]."'; 
			 const DATABASE = '".$a_settingsArray[self::DB]."'; 
			 const DBPREFIX = 'live_'; 
			 const DBTESTPREFIX = 'test_';
		 }";
		
		//Try to open the file
		if (!$handle = fopen($this->m_fileName, 'w')) {
			LogHandler::logError("Cannot open file: " . $this->m_fileName);
			return false;
		}

		//Try to write to the bottom of our file
		if (!fwrite($handle, $confString)) {
			LogHandler::logError("Cannot write to file: " . $this->m_fileName);
			return false;
		}

		//Close file
		fclose($handle);
		return true;
	}
	
	/**
	 * Checks if we have a working DBSettings.php file by creating and terminating a connection
	 * @Access Public
	 * @Param -
	 * @Return Bool checks if DBSettings.php file exists
	 */
	public function isAllreadyInstalled(){
		//Check if DBSettings file exists
		if(file_exists($this->m_fileName) === true){
			return true;	
		}
		return false;
	}
	
	/**
	 * Creates table and a Admin user
	 * @Access Public
	 * @Param - 
	 * @Return Bool indicating if we were able to create tables and an admin user
	 */
	public function createAndPopulateTables(){
		require_once("Common/DBConnection.php");
		
		$this->m_DBConnection = new DBConnection();
		$this->m_userFileHandler = new UserFileHandler($this->m_DBConnection, DBSettings::DBPREFIX);
		$this->m_userHandler = new UserHandler($this->m_DBConnection, DBSettings::DBPREFIX);
		
		//Try to connect
		if($this->m_DBConnection->connect() === false){
			LogHandler::logError("InstallHandler Cannot Connect with given info");
			return false;
		}
		
		//Check if tables allready exists
		if(count($array = $this->m_userHandler->getUserByUserName("Admin")) === 1){
			return true;
		}
		
		
		//Try to create UserTabel
		if($this->m_userHandler->createTable() === false){
			LogHandler::logError("InstallHandler Cannot create Usertable");
			return false;
		}
		
		//Try to populate UserTabel
		if($this->m_userHandler->insertUser(User::create(0, "Admin", "310NuLc6xyjuc", "admin@test.nu", "318673369", 1)) === false){
			LogHandler::logError("InstallHandler Cannot Populate Usertabel");
			return false;
		}
		
		//Try to create Filetabel
		if($this->m_userFileHandler->createTable() === false){
			LogHandler::logError("InstallHandler Cannot create FileTabel");
			return false;
		} 
		
		$this->m_DBConnection->close();
		return true;
	}
	
	/**
	 * uninstalls the application and drops tables
	 * @access public
	 * @param -
	 * @return Bool
	 */
	public function uninstall(){
		require_once("Common/DBConnection.php");
		
		$this->m_DBConnection = new DBConnection();
		$this->m_DBConnection->connect();
		
		$this->m_userFileHandler = new UserFileHandler($this->m_DBConnection, DBSettings::DBPREFIX);
		$this->m_userHandler = new UserHandler($this->m_DBConnection, DBSettings::DBPREFIX);
		$this->m_loginHandler = new LoginHandler($this->m_userHandler);
		
		//Logout user
		$this->m_loginHandler->logOut();
		
		//Remove all files uploaded by users
		$allUsersArray = $this->m_userHandler->getAllUsers();
		foreach($allUsersArray as $user){
			$this->m_userFileHandler->deleteAllFilesByUserId($user->userId);
		}
		
		//Remove tables
		$this->m_userFileHandler->removeTable();
		$this->m_userHandler->removeTable();
		
		//Remove DBSettings document
		unlink($this->m_fileName);
		
		$this->m_DBConnection->close();
		return true;
	}

}